package cn.tedu.wms.product.service.impl;

import cn.tedu.wms.product.ex.ServiceException;
import cn.tedu.wms.product.mapper.SupplierMapper;
import cn.tedu.wms.product.pojo.dto.SupplierAddNewDTO;
import cn.tedu.wms.product.pojo.entity.Supplier;
import cn.tedu.wms.product.pojo.vo.SupplierListItemVO;
import cn.tedu.wms.product.pojo.vo.SupplierStandardVO;
import cn.tedu.wms.product.service.ISupplierService;
import cn.tedu.wms.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SupplierServiceImpl implements ISupplierService {

    @Autowired
    SupplierMapper mapper;

    @Override
    public void addNew(SupplierAddNewDTO addNewDTO) {
        log.info("开始处理新增供应商,接收的数据{}",addNewDTO);
        String name=addNewDTO.getName();
        int rows = mapper.countByName(name);
        if(rows>0){
            String message="添加失败,供应商已存在";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Supplier supplier=new Supplier();
        BeanUtils.copyProperties(addNewDTO,supplier);
        rows = mapper.insert(supplier);
        if(rows<1){
            String message="服务器忙,请稍后重试";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void insertBatch(List<SupplierAddNewDTO> addNewList) {

    }

    @Override
    public void deleteById(Long id) {
        int rows = mapper.countById(id);
        if (rows<1){
            String message="删除失败,该数据不存在";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        rows = mapper.deleteById(id);
        if(rows!=1){
            String message="服务器忙,请稍后重试";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateById(SupplierStandardVO supplierStandardVO) {
        Long id=supplierStandardVO.getId();
        int rows = mapper.countById(id);
        if (rows<1){
            String message="修改失败,该数据不存在";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        Supplier supplier=new Supplier();
        BeanUtils.copyProperties(supplierStandardVO,supplier);
        rows = mapper.updateById(supplier);
        if(rows!=1){
            String message="服务器忙,请稍后重试";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public void countById(Long id) {

    }

    @Override
    public SupplierStandardVO getStandardById(Long id) {
        SupplierStandardVO queryResult = mapper.getStandardById(id);
        if(queryResult==null){
            String message="查询失败,数据不存在";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_SELECT,message);
        }
        return queryResult;
    }

    @Override
    public List<SupplierListItemVO> list() {
        return mapper.list();
    }
}
